package com.lanou.myim.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lanou.myim.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/2/29.
 */
public class WelcomeActivity extends BaseActivity {
    private Handler handler1;
    private SharedPreferences first;
    private SharedPreferences.Editor editor;
    private ViewPager viewPager;
    private List<ImageView> images;
    private List<View> dots;
    private ViewPagerAdapter adapter;
    private int[] imageIds = {R.mipmap.bl, R.mipmap.hy,
            R.mipmap.mh, R.mipmap.hyh};
    private int[] dotIds = {R.id.dot_0, R.id.dot_1, R.id.dot_2, R.id.dot_3};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        first = getSharedPreferences("phone", Context.MODE_PRIVATE);
        //判断是不是首次登陆
        if (first.getBoolean("first Start", true)) {
            editor = first.edit();
            //将登陆标志位设置为false,下次登录时不再显示首次登陆界面
            editor.putBoolean("first Start", false);
            editor.commit();
            setContentView(R.layout.activity_welcome);
            viewPager = (ViewPager) findViewById(R.id.vp_introduce);
            initData();
            adapter = new ViewPagerAdapter();
            viewPager.setAdapter(adapter);
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    for (int i = 0; i < 4; i++) {
                        dots.get(i).setBackgroundColor(Color.WHITE);
                    }
                    dots.get(position).setBackgroundColor(0XFFFFCC66);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });


        } else {
            setContentView(R.layout.activity_splash);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                        handler1.sendEmptyMessage(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();


            handler1 = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message message) {
                    Intent intent = new Intent(WelcomeActivity.this, LogInActivity.class);
                    startActivity(intent);
                    finish();
                    return false;
                }
            });
        }
    }

    private void initData() {
        images = new ArrayList<>();
        dots = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imageIds[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            images.add(imageView);
            View view = findViewById(dotIds[i]);
            dots.add(view);
        }
        dots.get(0).setBackgroundColor(Color.RED);
        images.get(3).setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {
                Intent intent = new Intent(WelcomeActivity.this, LogInActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup view, int position, Object object) {
            view.removeView(images.get(position));

        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            view.addView(images.get(position));
            return images.get(position);
        }


    }


}
